package com.yf.controller;

import com.yf.dto.Message;
import com.yf.dto.Page;
import com.yf.dto.Result;
import com.yf.entity.Student;
import com.yf.myException.ExceptionEnum;
import com.yf.myException.NewException;
import com.yf.service.StudentService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ResponseBody
@CrossOrigin
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 添加学员
     * @param student
     * @return
     */
    @RequiresRoles(value = {"teacher"})
    @RequestMapping(value = "/addStudent",method = RequestMethod.POST)
    public Result addStudent(Student student){

        try {
            studentService.addStudent(student);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_ADD);
        }
    }

    /**
     * 删除学员
     * @param studentId
     * @return
     */
    @RequiresRoles(value = {"teacher"})
    @RequestMapping(value = "/deleteStudent")
    public Result deleteStudent(Integer studentId){

        try {
            studentService.deleteStudent(studentId);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_DELETE);
        }
    }

    /**
     * 更新学员信息
     * @param student
     * @return
     */
    @RequiresRoles(value = {"teacher"})
    @RequestMapping(value = "/updateStudent",method = RequestMethod.POST)
    public Result updateStudent(Student student){

        try {
            studentService.updateStudent(student);
            Result result = new Result();
            result.setMessage(new Message());
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_UPDATE);
        }
    }

    /**
     * 通过学员ID查询学员信息
     * @param studentId
     * @return
     */
    @RequiresRoles(value = {"teacher"})
    @RequestMapping(value = "/getStudentByStuId")
    public Result getStudentById(Integer studentId){

        try {
            Student studentById = studentService.getStudentById(studentId);
            Result result = new Result();
            result.setMessage(new Message());
            result.setObj(studentById);
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_GET);
        }
    }

    /**
     * 分页查询
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequiresRoles(value = {"teacher"})
    @RequestMapping(value = "/getStudentPage")
    public Result getStudentPage(Integer pageNo,Integer pageSize){

        try {
            Page studentPage = studentService.getStudentPage(pageNo, pageSize);
            Result result = new Result();
            result.setMessage(new Message());
            result.setObj(studentPage);
            return result;
        } catch (Exception e) {
            throw new NewException(ExceptionEnum.ERROR_GET);
        }
    }



}
